﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YamlDotNet.Serialization.NamingConventions;
using YamlDotNet.Serialization;
using Silk.NET.OpenGL;

namespace Meow.Core;

public class Rd_VertexPrototyeDef : Def
{
    /// <summary>
    /// The Segments of Vertex data, eg: Pos(3 * float),UV(2 * float)...
    /// </summary>
    public int[] Segments = Array.Empty<int>();

    public VertexAttribPointerType[] Types = Array.Empty<VertexAttribPointerType>();


    public override bool Equals(object? obj)
    {
        if (obj is Rd_VertexPrototyeDef other)
        {
            return other.Segments.SequenceEqual(Segments) && other.Types.SequenceEqual(Types);
        }
        return false;
    }

}

public class Rd_VertexPrototype : MeowRes
{
    public readonly Rd_VertexPrototyeDef Def;

    public Rd_Context currentContext;
    public uint currentVAO;
    public int Hash { get; private set; }
    public override int GetHashCode()
    {
        if (Hash == 0)
        {
            int c = 1;
            foreach (var v in Def.Segments)
            {
                c *= 10;
                Hash += v.GetHashCode() * c;
            }
            foreach (var v in Def.Types)
            {
                c *= 10;
                Hash += v.GetHashCode() * c;
            }
        }
        return Hash;
    }

    public Rd_VertexPrototype(string defpath)
    {
        Def = new DeserializerBuilder().WithNamingConvention(UnderscoredNamingConvention.Instance).Build().Deserialize<Rd_VertexPrototyeDef>(File.ReadAllText(defpath));
        Hash = GetHashCode();
    }

    public Rd_VertexPrototype(Rd_VertexPrototyeDef def)
    {
        Def = def;
        Hash = GetHashCode();
    }
    public override bool Save(string path)
    {
        return Def.Save(path);
    }

    public override bool Equals(object? obj)
    {
        if (obj is Rd_VertexPrototype other)
        {
            return other.Def.Equals(Def);
        }
        return false;
    }
}
